<?php

namespace app\admin\controller;

use app\admin\controller\CommonController;
use think\Validate;
use think\Db;
use app\admin\model\NewPeopleToItem;
use app\admin\model\Order;

class NewpeopleitemController extends CommonController {
    /*
     * 新人专享列表
     * author xfwed 
     * date 2019.12.23
     */

    public function newPeopleItemList() {
        $item_name = input('param.item_name');
        $perPage = input('param.perPage');
        $page = input('param.page');

        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;
        if($item_name){
            $where['i.item_name'] = ['like','%'.$item_name.'%'];
        }
        $where['n.is_del'] = 1 ;
        $list = NewPeopleToItem::alias('n')
                ->join('item i','i.id = n.item_id','left')
                ->where($where)
                ->field('n.id,n.item_id,n.new_people_price,n.add_time,i.item_name,i.item_price')
                ->paginate($perPage);
        //新人专享订单23040，实付金额￥394.05，已返美容金239585.03
        $orderIds = Order::where('pay_status',2)
                ->where('first_exemption',2)
                ->column('id');
        $orderCount = count($orderIds);
        $orderMoney = Order::where('pay_status',2)->where('first_exemption',2)->sum('pay_money+balance_money');
//        $redPacketCtmIds = Db::name('red_packet_ctm')->where('order_id','in',$orderIds)->column('id');
//        if($redPacketCtmIds){
//            $redMoney = Db::name('redpacket')->where('packet_ctm_id','in',$redPacketCtmIds)->sum('money');
//        }  else {
//            $redMoney = 0;
//        }
        
        $data['orderCount'] = $orderCount;
        $data['orderMoney'] = $orderMoney;
        //$data['redMoney'] = $redMoney;
       //2021年6月1日前， 新人专享订单23040，实付金额￥394.05
        $orderCount6 = Order::where('pay_status',2)
                ->where('first_exemption',2)
                ->where('pay_time','<','2021-06-01')
                ->count();
        $orderMoney6 = Order::where('pay_status',2)
                ->where('first_exemption',2)
                ->where('pay_time','<','2021-06-01')
                ->sum('pay_money+balance_money');
        $data['orderCount6'] = $orderCount6;
        $data['orderMoney6'] = $orderMoney6;
        $data['list'] = $list;
        return $this->ajaxReturn(0, '查询成功', $data);
    }
    
    /*
     * 下架新人专享
     * author xfwed 
     * date 2019.12.23
     */

    public function soldOutNewPeopleItem() {
        $id = input('param.id');
        
        $save = NewPeopleToItem::where('id',$id)->update(['is_del'=> 2]);
        if($save){
            return $this->ajaxReturn(0, '下架成功', NULL);
        }  else {
            return $this->ajaxReturn(3, '下架失败', null);
        }
        
    }
    /*
     * 添加新人专享
     * author xfwed 
     * date 2019.12.23
     */

    public function addNewPeopleItem() {
        $item_id = input('param.item_id');
        $new_people_price = input('param.new_people_price');
        
        $row = NewPeopleToItem::where('item_id',$item_id)
                //->where('new_people_price',$new_people_price)
                ->where('is_del',1)
                ->find();
        if($row){
            return $this->ajaxReturn(3, '该项目已经是新人专享项目', null);
        }
        $save = NewPeopleToItem::insert([
            'item_id'=> $item_id,
            'new_people_price' =>$new_people_price,
            'add_time' => date('Y-m-d H:i:s'),
            'is_del' => 1,
            ]);
        if($save){
            return $this->ajaxReturn(0, '添加成功', NULL);
        }  else {
            return $this->ajaxReturn(3, '添加失败', null);
        }
        
    }
    

    
}
